package com.qimei.onerice.dao.impl;

import com.qimei.onerice.dao.DishDao;
import com.qimei.onerice.entity.Dish;
import com.qimei.onerice.until.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DishDaoImpl implements DishDao {

	private static final String INSERT_DISH = "INSERT INTO dishes (dish_id, name, description, price, category_id, dish_image) VALUES (?, ?, ?, ?, ?)";
	private static final String SELECT_DISH_BY_ID = "SELECT * FROM dishes WHERE dish_id = ?";
	private static final String SELECT_ALL_DISHES = "SELECT * FROM dishes";
	private static final String UPDATE_DISH = "UPDATE dishes SET name = ?, description = ?, price = ?, category_id = ?, dish_image = ? WHERE dish_id = ?";
	private static final String DELETE_DISH = "DELETE FROM dishes WHERE dish_id = ?";
	private static final String SELECT_DISH_BY_CATEGORY_ID = "SELECT * FROM dishes WHERE category_id = ?";
	private static final String SELECT_GAME_BY_NAME_LIKE = "SELECT * FROM dishes WHERE name LIKE ?";

	@Override
	public int addDish(Dish dish) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(INSERT_DISH)) {
			preparedStatement.setInt(1, dish.getDishId());
			preparedStatement.setString(2, dish.getName());
			preparedStatement.setString(3, dish.getDescription());
			preparedStatement.setDouble(4, dish.getPrice());
			preparedStatement.setInt(5, dish.getCategoryId());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public List<Dish> getDishListByNameLike(String dishName) {
		List<Dish> dishs = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_DISH_BY_CATEGORY_ID)) {
			// 设置查询参数
			preparedStatement.setString(1, "%" + dishName + "%");

			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				while (resultSet.next()) {
					dishs.add(createDishFromResultSet(resultSet));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return dishs;
	}

	@Override
	public Dish getDishById(int dishId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_DISH_BY_ID)) {
			preparedStatement.setInt(1, dishId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createDishFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}

	@Override
	public List<Dish> getAllDishes() {
		List<Dish> dishes = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_DISHES);
		     ResultSet resultSet = preparedStatement.executeQuery()) {
			while (resultSet.next()) {
				dishes.add(createDishFromResultSet(resultSet));
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return dishes;
	}

	@Override
	public List<Dish> getDishListByCategoryId(int dishCategoryId) {
		List<Dish> dishs = new ArrayList<>();

		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_DISH_BY_CATEGORY_ID)) {
			// 设置查询参数
			preparedStatement.setLong(1, dishCategoryId);

			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				while (resultSet.next()) {
					dishs.add(createDishFromResultSet(resultSet));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return dishs;
	}

	@Override
	public int updateDish(Dish dish) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_DISH)) {
			preparedStatement.setString(1, dish.getName());
			preparedStatement.setString(2, dish.getDescription());
			preparedStatement.setDouble(3, dish.getPrice());
			preparedStatement.setInt(4, dish.getCategoryId());
			preparedStatement.setInt(5, dish.getDishId());
			preparedStatement.setString(6, dish.getDishImage());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public int deleteDish(int dishId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(DELETE_DISH)) {
			preparedStatement.setInt(1, dishId);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	// Helper 方法，从 ResultSet 中创建 Dish 对象
	private Dish createDishFromResultSet(ResultSet resultSet) throws SQLException {
		Dish dish = new Dish();
		dish.setDishId(resultSet.getInt("dish_id"));
		dish.setName(resultSet.getString("name"));
		dish.setDescription(resultSet.getString("description"));
		dish.setPrice(resultSet.getDouble("price"));
		dish.setCategoryId(resultSet.getInt("category_id"));
		dish.setDishImage(resultSet.getString("dish_image"));
		return dish;
	}
}
